<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>类型系统</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.intro.html">« 简介</a></li>
      <li style="float: right;"><a href="language.types.null.html">NULL »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>类型系统</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.type-system" class="sect1">
 <h2 class="title">类型系统</h2>

 <p class="para">
  PHP uses a nominal type system with a strong behavioral subtyping relation.
  The subtyping relation is checked at compile time whereas the verification of
  types is dynamically checked at run time.
 </p>

 <p class="para">
  PHP 的类型系统支持各种基础类型，可以组合在一起创建更加复杂的类型。其中一些类型可以写成<a href="language.types.declarations.html" class="link">类型声明</a>。
 </p>

 <div class="sect2" id="language.types.type-system.base">
  <h3 class="title">基本类型</h3>
  <p class="para">
   一些基础类型是内置类型，跟语言紧密集成，不能用用户定义类型重现（reproduced）。
  </p>

  <p class="para">
   基础类型列表是：
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">内置类型</span>
     <ul class="itemizedlist">
      <li class="listitem">
       <span class="simpara"><span class="type">null</span> 类型</span>
      </li>
      <li class="listitem">
       <span class="simpara">
        标量类型：
       </span>
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara"><span class="type">bool</span> 类型</span>
        </li>
        <li class="listitem">
         <span class="simpara"><span class="type">int</span> 类型</span>
        </li>
        <li class="listitem">
         <span class="simpara"><span class="type">float</span> 类型</span>
        </li>
        <li class="listitem">
         <span class="simpara"><span class="type">string</span> 类型</span>
        </li>
       </ul>
      </li>
      <li class="listitem">
       <span class="simpara"><span class="type">array</span> 类型</span>
      </li>
      <li class="listitem">
       <span class="simpara"><span class="type">object</span> 类型</span>
      </li>
      <li class="listitem">
       <span class="simpara"><span class="type">resource</span> 类型</span>
      </li>
      <li class="listitem">
       <span class="simpara"><span class="type"><span class="type never">never</span></span> 类型</span>
      </li>
      <li class="listitem">
       <span class="simpara"><span class="type"><span class="type void">void</span></span> 类型</span>
      </li>
      <li class="listitem">
       <span class="simpara">
        <a href="language.types.relative-class-types.html" class="link">相对类类型</a>：<span class="type"><span class="type self">self</span></span>、<span class="type"><span class="type parent">parent</span></span>
        和 <span class="type"><span class="type static">static</span></span>
       </span>
      </li>
     </ul>
    </li>
    <li class="listitem">
     <span class="simpara">
      <a href="language.types.literal.html" class="link">字面量类型</a>
     </span>
     <ul class="itemizedlist">
      <li class="listitem">
       <span class="simpara"><span class="type"><span class="type false">false</span></span></span>
      </li>
      <li class="listitem">
       <span class="simpara"><span class="type"><span class="type true">true</span></span></span>
      </li>
     </ul>
    </li>
    <li class="listitem">
     <span class="simpara">
      用户定义的类型（通常称为类类型）
     </span>
     <ul class="itemizedlist">
      <li class="listitem">
       <span class="simpara"><a href="language.oop5.interfaces.html" class="link">接口</a></span>
      </li>
      <li class="listitem">
       <span class="simpara"><a href="language.oop5.basic.html#language.oop5.basic.class" class="link">类</a></span>
      </li>
      <li class="listitem">
       <span class="simpara"><a href="language.types.enumerations.html" class="link">枚举</a></span>
      </li>
     </ul>
    </li>
    <li class="listitem">
     <span class="simpara"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> 类型</span>
    </li>
   </ul>
  </p>
 </div>

 <div class="sect2" id="language.types.type-system.composite">
  <h3 class="title">复合类型</h3>
  <p class="para">
   可以将简单类型组合为复合类型。PHP 允许使用以下方式组合类型：
  </p>

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     类类型（接口和类名）的交集。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     类型联合。
    </span>
   </li>
  </ul>

  <div class="sect3" id="language.types.type-system.composite.intersection">
   <h4 class="title">交集类型</h4>
   <p class="para">
    交集类型接受满足多个类类型声明的值，而不是单个值。交集类型中的每个类型由 <code class="literal">&amp;</code>
    符号连接。因此，类型 <code class="literal">T</code>、<code class="literal">U</code> 和 <code class="literal">V</code>
    组成的交集类型将写成 <code class="literal">T&amp;U&amp;V</code>。
   </p>
  </div>

  <div class="sect3" id="language.types.type-system.composite.union">
   <h4 class="title">联合类型</h4>
   <p class="para">
    联合类型接受多个不同类型的值，而不是单个类型。联合类型中的每个类型由 <code class="literal">|</code>
    符号连接。因此类型 <code class="literal">T</code>、<code class="literal">U</code> 和 <code class="literal">V</code>
    的联合类型写成 <code class="literal">T|U|V</code>。如果其中一种类型是交集类型，需要使用括号括起来，在
    <abbr title="Disjunctive Normal Form">DNF</abbr> 中写成：<code class="literal">T|(X&amp;Y)</code>。
   </p>
  </div>
 </div>

 <div class="sect2" id="language.types.type-system.alias">
  <h3 class="title">类型别名</h3>

  <p class="para">
   PHP 支持两种类型别名：<span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> 和 <span class="type"><a href="language.types.iterable.html" class="type iterable">iterable</a></span>，分别对应
   <code class="literal">object|resource|array|string|float|int|bool|null</code> 和
   <code class="literal">Traversable|array</code> 的 <a href="language.types.type-system.html#language.types.type-system.composite.union" class="link">联合类型</a>。
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    PHP 不支持用户定义类型别名。
   </span>
  </p></blockquote>
 </div>

</div></div></div></body></html>